package lwlk.controller;

import lwlk.model.User;
import lwlk.service.i.IUserService;
import org.beetl.core.Template;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class HomeController {

    @Autowired
    @Qualifier("userService")
    private IUserService userService;

    @Autowired
    private SQLManager sqlManager;

    @Autowired
    private BeetlGroupUtilConfiguration configuration;

    @RequestMapping(value = "/home/{operate}", method = RequestMethod.GET, produces = {"application/json; charset=UTF-8"})
    public String home(@PathVariable String operate) {

        return operate;
    }

    @RequestMapping(value = "/page/{var}", method = RequestMethod.GET, produces = {"application/json; charset=UTF-8"})
    public String page(@PathVariable String var) {

        Template template = configuration.getGroupTemplate().getTemplate("/beetl/test.html");

        template.binding("test", var);

        Map<String, Object> map = new HashMap<>();

        map.put("id", 1);

        map.put("userName", "wahaha");

        List<User> userList = sqlManager.select("gloable.selectAll", User.class, map);

        template.binding("test2", userList.stream().map(User::getAccount).collect(Collectors.joining(", ")));

        return template.render();
    }
}